﻿/// Name - MyHealth/ConfirmRegistration.cs
/// Description - Allows input of registration code and validates user email address.
/// Programmer - Dan Yatzeck
/// Date coded - 10/30/2011
/// Arguments - None
/// Variables -
///     _presenter - Object linking to parent Presenter
///     currentUser - Current user object
/// Files accessed by this code artifact - None
/// Files changed by this code artifact - None
/// I/O -
///     Input - Session variable with current User object or QueryString with email address and reg code
///     Output - Web page, session variable with updated current User object
/// Error Handling - None
/// Modifications -
///     1.0 - Initial coding
/// Known Faults -
///     1.0 - None known

using System;
using Microsoft.Practices.ObjectBuilder;
using Common;

namespace CSC440TeamOne.MyHealth.Views
{
    /// <summary>
    /// Registration confirmation page
    /// </summary>
	public partial class ConfirmRegistration : Microsoft.Practices.CompositeWeb.Web.UI.Page, IConfirmRegistrationView
	{
		private ConfirmRegistrationPresenter _presenter;
        private User currentUser;

        /// <summary>
        /// Page Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		protected void Page_Load(object sender, EventArgs e)
		{
            // Get current user
            if (Session["currentUser"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            else
            {
                this.currentUser = Session["currentUser"] as User;
            }

            // Initial load (no postback)
			if (!this.IsPostBack)
			{
				this._presenter.OnViewInitialized();
                ViewState["emailAddress"] = Request.QueryString["emailAddress"];
			}
			this._presenter.OnViewLoaded();
		}

        /// <summary>
        /// When this object is instantiated, create the presenter link
        /// </summary>
		[CreateNew]
		public ConfirmRegistrationPresenter Presenter
		{
			get
			{
				return this._presenter;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException("value");

				this._presenter = value;
				this._presenter.View = this;
			}
		}

        /// <summary>
        /// Submit click event handler
        /// Validates registration string, modifies panels on the page to display success or failure.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void submit_Click(object sender, EventArgs e)
        {
            if (Common.Cryptography.VerifyCypherText(txtRegistrationCode.Text))
            {
                // Verify the email address exists in the ViewState
                if (ViewState["emailAddress"] == null)
                {
                    Response.Redirect("Default.aspx", true);
                }

                string emailAddress = ViewState["emailAddress"].ToString();

                // Make sure the email address has content
                if (String.IsNullOrEmpty(emailAddress))
                {
                    Response.Redirect("Default.aspx", true);
                }

                // Attempt decryption
                if (Common.Cryptography.DecryptString(txtRegistrationCode.Text, "CSC440TeamOne") == emailAddress)
                {
                    this.currentUser.RegistrationConfirmed = true;
                    Session["currentUser"] = this.currentUser;

                    // Success
                    pnlRegistrationCode.Visible = false;
                    pnlFailure.Visible = false;
                    pnlThankYou.Visible = true;
                }
                else
                {
                    // Failure
                    pnlRegistrationCode.Visible = true;
                    pnlFailure.Visible = true;
                    pnlThankYou.Visible = false;
                }
            }
            else
            {
                // Cyphertext not valid
                pnlRegistrationCode.Visible = true;
                pnlFailure.Visible = true;
                pnlThankYou.Visible = false;
            }
        }
}
}

